package gameos.menu.shop;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener.ChangeEvent;

import gameos.core.Asset;
import gameos.core.GameOS;
import gameos.core.IScreen;
import gameos.core.IVisible;
import gameos.menu.IEntityMenu;

public class AboutItemUi extends IEntityMenu{

	public AboutItemUi (GameOS game, IScreen screen) {
		super(game, screen);
		// TODO Auto-generated constructor stub
	}

	Table table;
	Label cost;
	Image[] details = new Image[15];
	ImageButton buy;
	ImageButton equip;
	ImageButton unequip;
	@Override
	public void start () {
		// TODO Auto-generated method stub
		table = game.stageHelper.createTable(game.X(561), game.Y(490-378), game.X(744-561), game.Y(378-171));
		for(int i=0;i<15;i++)
		{
			details[i] = game.stageHelper.createImage(Asset.details[i], game.X(5), game.Y(50));
			table.addActor(details[i]);
			details[i].setVisible(false);
		}
	   cost = game.stageHelper.createLabel("9000$", Color.ORANGE, game.X(5), game.Y(140));
	   table.addActor(cost);
	   equip = game.stageHelper.createImageButton(Asset.equip, Asset.equip, 0, 0);
	   buy = game.stageHelper.createImageButton(Asset.buy, Asset.buy, 0, 0);
	   unequip = game.stageHelper.createImageButton(Asset.unequip, Asset.unequip, 0, 0);
	   table.addActor(equip);
	   table.addActor(unequip);
	   table.addActor(buy);
	   buy.addListener(new ChangeListener() {			
			@Override
			public void changed (ChangeEvent event, Actor actor) {
				// TODO Auto-generated method stub
				if(_screen.shopUi.shopData.tab == 1)
				{	
					if(_screen.shopUi.shopData.item>0)
					{
						if(game.dataSave.money >= _screen.shopUi.shopData.tab1Item[_screen.shopUi.shopData.item-1].price){
							game.dataSave.money -= _screen.shopUi.shopData.tab1Item[_screen.shopUi.shopData.item-1].price;
							game.dataSave.saveAll();
							_screen.shopUi.shopData.tab1Item[_screen.shopUi.shopData.item-1].isBuy = true;
							_screen.shopUi.refresh();
						}
						else
						{
							if(game.android!=null)
								game.android.showToast("Bạn không đủ xu để mua item này!");
						}
					}
				}
				if(_screen.shopUi.shopData.tab == 2)
				{	
					if(_screen.shopUi.shopData.item>0)
					{
						if(game.dataSave.money >= _screen.shopUi.shopData.tab2Item[_screen.shopUi.shopData.item-1].price){
							game.dataSave.money -= _screen.shopUi.shopData.tab2Item[_screen.shopUi.shopData.item-1].price;
							game.dataSave.saveAll();
						_screen.shopUi.shopData.tab2Item[_screen.shopUi.shopData.item-1].isBuy = true;
						_screen.shopUi.refresh();
						}
						else
						{
							if(game.android!=null)
								game.android.showToast("Bạn không đủ xu để mua item này!");
						}
					}
				}
				if(_screen.shopUi.shopData.tab == 3)
				{	
					if(_screen.shopUi.shopData.item>0)
					{
						if(game.dataSave.money >= _screen.shopUi.shopData.tab3Item[_screen.shopUi.shopData.item-1].price){
							game.dataSave.money -= _screen.shopUi.shopData.tab3Item[_screen.shopUi.shopData.item-1].price;
							game.dataSave.saveAll();
						_screen.shopUi.shopData.tab3Item[_screen.shopUi.shopData.item-1].isBuy = true;
						_screen.shopUi.refresh();
						}
						else
						{
							if(game.android!=null)
								game.android.showToast("Bạn không đủ xu để mua item này!");
						}
					}
				}
			}
		});
	   
	   equip.addListener(new ChangeListener() {			
			@Override
			public void changed (ChangeEvent event, Actor actor) {
				// TODO Auto-generated method stub
				if(_screen.shopUi.shopData.tab == 1)
				{	
					if(_screen.shopUi.shopData.item>0)
					{
						_screen.shopUi.shopData.tab1Item[0].isEquipe = false;
						_screen.shopUi.shopData.tab1Item[1].isEquipe = false;
						_screen.shopUi.shopData.tab1Item[2].isEquipe = false;
						_screen.shopUi.shopData.tab1Item[3].isEquipe = false;
						_screen.shopUi.shopData.tab1Item[4].isEquipe = false;
						_screen.shopUi.shopData.tab1Item[_screen.shopUi.shopData.item-1].isEquipe = true;
						if(_screen.shopUi.shopData.item == 4)
						{
							game.gameScreen.nvc.delta = 1;
						}
						else if(_screen.shopUi.shopData.item == 5)
						{
							game.gameScreen.nvc.delta = 2;
						}
						else if(_screen.shopUi.shopData.item == 3)
							game.gameScreen.nvc.delta = 0;
						else 
							game.gameScreen.nvc.delta = -1;
						_screen.shopUi.refresh();
					}
				}
				if(_screen.shopUi.shopData.tab == 2)
				{	
					if(_screen.shopUi.shopData.item>0)
					{
						_screen.shopUi.shopData.tab2Item[0].isEquipe = false;
						_screen.shopUi.shopData.tab2Item[1].isEquipe = false;
						_screen.shopUi.shopData.tab2Item[2].isEquipe = false;
						_screen.shopUi.shopData.tab2Item[_screen.shopUi.shopData.item-1].isEquipe = true;
						_screen.shopUi.refresh();
					}
				}
				if(_screen.shopUi.shopData.tab == 3)
				{	
					if(_screen.shopUi.shopData.item>0)
					{
						_screen.shopUi.shopData.tab3Item[0].isEquipe = false;
						_screen.shopUi.shopData.tab3Item[1].isEquipe = false;
						_screen.shopUi.shopData.tab3Item[2].isEquipe = false;
						_screen.shopUi.shopData.tab3Item[3].isEquipe = false;
						_screen.shopUi.shopData.tab3Item[_screen.shopUi.shopData.item-1].isEquipe = true;
						_screen.shopUi.refresh();
					}
				}
			}
		});
	   
	   unequip.addListener(new ChangeListener() {			
			@Override
			public void changed (ChangeEvent event, Actor actor) {
				// TODO Auto-generated method stub
				if(_screen.shopUi.shopData.tab == 1)
				{	
					if(_screen.shopUi.shopData.item>0)
					{
						_screen.shopUi.shopData.tab1Item[_screen.shopUi.shopData.item-1].isEquipe = false;
						_screen.shopUi.refresh();
					}
				}
				if(_screen.shopUi.shopData.tab == 2)
				{	
					if(_screen.shopUi.shopData.item>0)
					{
						_screen.shopUi.shopData.tab2Item[_screen.shopUi.shopData.item-1].isEquipe = false;
						_screen.shopUi.refresh();
					}
				}
				if(_screen.shopUi.shopData.tab == 3)
				{	
					if(_screen.shopUi.shopData.item>0)
					{
						_screen.shopUi.shopData.tab3Item[_screen.shopUi.shopData.item-1].isEquipe = false;
						_screen.shopUi.refresh();
					}
				}
			}
		});
	}	
	
	public void refesh()
	{	
		for(int i=0;i<15;i++)
		{
			details[i].setVisible(false);
		}
		if(_screen.shopUi.shopData.tab == 1){
			if((_screen.shopUi.shopData.tab-1)+_screen.shopUi.shopData.item==4)
			{
				details[13].setVisible(true);
			}		
			else if((_screen.shopUi.shopData.tab-1)+_screen.shopUi.shopData.item==5)
			{
				details[14].setVisible(true);
			}
			else
				details[(_screen.shopUi.shopData.tab-1)+_screen.shopUi.shopData.item].setVisible(true);
		}
		if(_screen.shopUi.shopData.tab == 2)
			details[4+_screen.shopUi.shopData.item].setVisible(true);
		if(_screen.shopUi.shopData.tab == 3)
			details[8+_screen.shopUi.shopData.item].setVisible(true);
		equip.setVisible(false);
		unequip.setVisible(false);
		buy.setVisible(false);
		cost.setVisible(false);
		if(_screen.shopUi.shopData.tab == 1)
		{	
			if(_screen.shopUi.shopData.item>0){
			if(_screen.shopUi.shopData.tab1Item[_screen.shopUi.shopData.item-1].isBuy)
			{
				if(_screen.shopUi.shopData.tab1Item[_screen.shopUi.shopData.item-1].isEquipe)
				{
					unequip.setVisible(true);
				}
				else					
				{	
					equip.setVisible(true);
				}
			}
			else
			{
				cost.setText(_screen.shopUi.shopData.tab1Item[_screen.shopUi.shopData.item-1].price+"");
				cost.setVisible(true);
				buy.setVisible(true);
			}
			}
		}
		if(_screen.shopUi.shopData.tab == 2)
		{	
			if(_screen.shopUi.shopData.item>0){
			if(_screen.shopUi.shopData.tab2Item[_screen.shopUi.shopData.item-1].isBuy)
			{
				if(_screen.shopUi.shopData.tab2Item[_screen.shopUi.shopData.item-1].isEquipe)
				{
					unequip.setVisible(true);
				}
				else					
				{	
					equip.setVisible(true);
				}
			}
			else
			{
				cost.setText(_screen.shopUi.shopData.tab2Item[_screen.shopUi.shopData.item-1].price+"");
				cost.setVisible(true);
				buy.setVisible(true);
			}
			}
		}
		if(_screen.shopUi.shopData.tab == 3)
		{	
			if(_screen.shopUi.shopData.item>0){
			if(_screen.shopUi.shopData.tab3Item[_screen.shopUi.shopData.item-1].isBuy)
			{
				if(_screen.shopUi.shopData.tab3Item[_screen.shopUi.shopData.item-1].isEquipe)
				{
					unequip.setVisible(true);
				}
				else					
				{	
					equip.setVisible(true);
				}
			}
			else
			{
				cost.setText(_screen.shopUi.shopData.tab3Item[_screen.shopUi.shopData.item-1].price+"");
				cost.setVisible(true);
				buy.setVisible(true);
			}
			}
		}
		int xCost = (int)((game.X(744-561) - cost.getTextBounds().width)/2);
		cost.setX(xCost);
		//cost = game.stageHelper.createLabel("9000$", Color.ORANGE, xCost, game.Y(140));
	}

	@Override
	public void stop () {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void draw () {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void update (int delayTime) {
		// TODO Auto-generated method stub
		
	}

}
